package com.jzw.service.impl;

import com.jzw.enums.Sex;
import com.jzw.mapper.UsersMapper;
import com.jzw.pojo.Users;
import com.jzw.pojo.bo.UserBo;
import com.jzw.service.UserService;
import com.jzw.utils.DateUtil;
import com.jzw.utils.MD5Utils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private Sid sid;

    public static final String USER_FACE = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1545190233,2172456233&fm=26&gp=0.jpg";

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Users getUser(String id) {
        return usersMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveUser(Users users) {
        usersMapper.insert(users);
    }

    @Override
    public void updateUser(Users users) {
        usersMapper.updateByPrimaryKey(users);
    }

    @Override
    public void deleteUser(String id) {
        usersMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Boolean queryUserNameIsExist(String userName) {
        Example userExample = new Example(Users.class);
        Criteria criteria = userExample.createCriteria();

        criteria.andEqualTo("username", userName);
        Users users = usersMapper.selectOneByExample(userExample);

        return users != null;
    }

    @Override
    public void saveParent() {
        Users users = new Users();

        users.setId(UUID.randomUUID().toString());
        users.setUsername("parent");
        users.setPassword("123456");
        users.setBirthday(new Date());
        users.setFace("https://www.baidu.com");
        users.setSex(1);
        users.setCreatedTime(new Date());
        users.setUpdatedTime(new Date());
        users.setEmail("8331818318@qq.com");
        users.setMobile("19837385672");
        users.setNickname("patent-nick");
        users.setRealname("爸爸");

        usersMapper.insert(users);
    }

    @Override
    @Transactional
    public void saveChildren() {
        saveChildren1();
        System.out.println();
        saveChildren2();
    }

    public void saveChildren1() {
        Users users = new Users();

        users.setId("123");
        users.setUsername("children1");
        users.setPassword("123456");
        users.setBirthday(new Date());
        users.setFace("https://www.baidu.com");
        users.setSex(1);
        users.setCreatedTime(new Date());
        users.setUpdatedTime(new Date());
        users.setEmail("8331818318@qq.com");
        users.setMobile("19837385672");
        users.setNickname("children1-nick");
        users.setRealname("儿子1");

        usersMapper.insert(users);
    }

    public void saveChildren2() {
        Users users = new Users();

        users.setId(UUID.randomUUID().toString());
        users.setUsername("children2");
        users.setPassword("123456");
        users.setBirthday(new Date());
        users.setFace("https://www.baidu.com");
        users.setSex(1);
        users.setCreatedTime(new Date());
        users.setUpdatedTime(new Date());
        users.setEmail("8331818318@qq.com");
        users.setMobile("19837385672");
        users.setNickname("children2-nick");
        users.setRealname("儿子2");

        usersMapper.insert(users);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Users createUser(UserBo userBo) throws Exception {
        Users users = new Users();

        users.setUsername(userBo.getUsername());
        users.setPassword(MD5Utils.getMD5Str(userBo.getPassword()));

        //默认用户昵称同用户名
        users.setNickname(userBo.getUsername());
        //默认头像
        users.setFace(USER_FACE);
        //默认生日
        users.setBirthday(DateUtil.stringToDate("1900-01-01"));
        //默认性别 保密
        users.setSex(Sex.secret.type);
        users.setCreatedTime(new Date());
        users.setUpdatedTime(new Date());

        users.setId(sid.nextShort());

        usersMapper.insert(users);

        return users;
    }
}
